<%@page import="org.jivesoftware.util.LocaleUtils"%>
<%@page import="org.lzy.pt.bean.Server"%>
<%@page import="org.lzy.pt.ServerListManager"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.List"%>
<%@page import="org.jivesoftware.util.ParamUtils"%>
<%@page import="java.util.Collection" %>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

<%!
    final int DEFAULT_RANGE = 100;
    final int[] RANGE_PRESETS = {25, 50, 75, 100, 500, 1000, -1};
%>

<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager"  />
<% webManager.init(request, response, session, application, out ); %>

<html>
    <head>
        <title><fmt:message key="item.server.summary"></fmt:message></title>
        <meta name="pageID" content="server-summary"/>
    </head>
    <body>
    
<%  // Get parameters
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",webManager.getRowsPerPage("server-summary", DEFAULT_RANGE));

    if (request.getParameter("range") != null) {
        webManager.setRowsPerPage("server-summary", range);
    }

    // Get the user manager
    long userCount = ServerListManager.getInstance().getCount();

    // paginator vars
    int numPages = (int)Math.ceil((double)userCount/(double)range);
    int curPage = (start/range) + 1;
%>

<p>
<fmt:message key="server.summary.total_server" />:
<b><%= LocaleUtils.getLocalizedNumber(userCount) %></b> --

<%  if (numPages > 1) { %>servert:message key="global.showing" />
    <%= LocaleUtils.getLocalizedNumber(start+1) %>-<%= LocaleUtils.getLocalizedNumber(start+range > userCount ? userCount:start+range) %>,

<%  } %>

-- <fmt:message key="server.summary.servers_per_page" />:
<select size="1" onchange="location.href='server-summary.jsp?start=0&range=' + this.options[this.selectedIndex].value;">

    <% for (int aRANGE_PRESETS : RANGE_PRESETS) { %>

    <option value="<%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= userCount %><%}%>"
            <%= (aRANGE_PRESETS == range ? "selected" : "") %>><%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= userCount %><%}%>
    </option>

    <% } %>

</select>
</p>

<%  if (numPages > 1) { %>

    <p>
    <fmt:message key="global.pages" />:
    [
    <%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
        <a href="server-summary.jsp?start=0&range=<%= range %>">1</a> ...

    <%
        }
        int i;
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
        <a href="server-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
         class="<%= ((isCurrent) ? "jive-current" : "") %>"
         ><%= (i+1) %></a><%= sep %>

    <%  } %>

    <%  if (i < numPages) { %>

        ... <a href="server-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

    <%  } %>

    ]

    </p>

<%  } %>

<div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
    <tr>
        <th>&nbsp;</th>
        <th nowrap><fmt:message key="server.T_DomainName"></fmt:message></th>
        <th nowrap><fmt:message key="server.T_Port"></fmt:message></th>
<%--         <th nowrap><fmt:message key="server.T_PublicKey"></fmt:message></th> --%>
        <th nowrap><fmt:message key="server.T_Pwd"></fmt:message></th>
        <th nowrap><fmt:message key="server.summary.edit" /></th>
        <th nowrap><fmt:message key="global.delete" /></th>
    </tr>
</thead>
<tbody>

<%  // Print the list of users
    Collection<Server> servers = ServerListManager.getInstance().getServerList(start, range);
    if (servers.isEmpty()) {
%>
    <tr>
        <td align="center" colspan="7">
            <fmt:message key="server.summary.not_server" />
        </td>
    </tr>

<%
    }
    int i = start;
    for (Server server : servers) {
        i++;
%>
    <tr class="jive-<%= (((i%2)==0) ? "even" : "odd") %>">
        <td width="1%">
            <%= i %>
        </td>
        <td>
        	<%=server.getT_DomainName() %>
        </td>
        <td>
        	<%=server.getT_Port() %>
        </td>
<!--         <td> -->
<%--         	<%=server.getT_PublicKey() %> --%>
<!--         </td> -->
        <td>
        	<%=server.getT_Pwd()%>
        </td>
        <td width="1%" align="center">
            <a href="server-edit-form.jsp?T_DomainName=<%=server.getT_DomainName() %>&T_Port=<%=server.getT_Port() %>" title="<fmt:message key="global.click_edit" />">
            	<img src="images/edit-16x16.gif" width="16" height="16" border="0" alt="<fmt:message key="global.click_edit" />">
            </a>
        </td>
        <td width="1%" align="center" style="border-right:1px #ccc solid;">
            <a href="server-delete.jsp?T_DomainName=<%=server.getT_DomainName() %>&T_Port=<%=server.getT_Port() %>"
             title="<fmt:message key="global.click_delete" />"
             ><img src="images/delete-16x16.gif" width="16" height="16" border="0" alt="<fmt:message key="global.click_delete" />"></a>
        </td>
    </tr>

<%
    }
%>
</tbody>
</table>
</div>

<%  if (numPages > 1) { %>

    <p>
    <fmt:message key="global.pages" />:
    [
    <%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
        <a href="server-summary.jsp?start=0&range=<%= range %>">1</a> ...

    <%
        }
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
        <a href="server-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
         class="<%= ((isCurrent) ? "jive-current" : "") %>"
         ><%= (i+1) %></a><%= sep %>

    <%  } %>

    <%  if (i < numPages) { %>

        ... <a href="server-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

    <%  } %>

    ]

    </p>

<%  } %>

    </body>
</html>
